﻿using System.Data.SqlClient;
using GeneralLibs;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lists;

namespace ListForms
{
    public partial class FrmDanhMucDichVu : ListForms.FrmBaseListForm
    {
        public FrmDanhMucDichVu()
        {
            InitializeComponent();
            DanhMucVatTuHangHoa.Instance.Change += new DanhMucVatTuHangHoa.ChangeHandler(Instance_Change);
        }

        void Instance_Change()
        {
            LoadData();
        }

        public override void InitGridColumns()
        {
            DevExpress.XtraGrid.Columns.GridColumn colMa = new DevExpress.XtraGrid.Columns.GridColumn();
            colMa.Caption = "Mã";
            colMa.FieldName = "Ma";
            colMa.Name = "colMa";
            colMa.Visible = true;
            colMa.VisibleIndex = 0;

            DevExpress.XtraGrid.Columns.GridColumn colTen = new DevExpress.XtraGrid.Columns.GridColumn();
            colTen.Caption = "Tên";
            colTen.FieldName = "Ten";
            colTen.Name = "colTen";
            colTen.Visible = true;
            colTen.VisibleIndex = 1;

            DevExpress.XtraEditors.Controls.LookUpColumnInfo nhomInfo = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Ten", "Tên", 20);
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit repLookupTaiKhoan = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            repLookupTaiKhoan.Columns.Add(nhomInfo);
            repLookupTaiKhoan.ValueMember = "ID";
            repLookupTaiKhoan.DisplayMember = "Ma";
            repLookupTaiKhoan.DataSource = DanhMucNhomVatTuHangHoa.Instance.DataView;

            DevExpress.XtraGrid.Columns.GridColumn colNhomVatTuHangHoa = new DevExpress.XtraGrid.Columns.GridColumn();
            colNhomVatTuHangHoa.Caption = "Nhóm hàng hóa";
            colNhomVatTuHangHoa.FieldName = "NhomVatTuHangHoaID";
            colNhomVatTuHangHoa.Name = "colTaiKhoan";
            colNhomVatTuHangHoa.ColumnEdit = repLookupTaiKhoan;
            colNhomVatTuHangHoa.Visible = true;
            colNhomVatTuHangHoa.VisibleIndex = 3;

            DevExpress.XtraGrid.Columns.GridColumn colGia = new DevExpress.XtraGrid.Columns.GridColumn();
            colGia.Caption = "Giá bán lẻ";
            colGia.FieldName = "GiaBia";
            colGia.Name = "colGiaBia";
            colGia.Visible = true;
            colGia.VisibleIndex = 4;

            DevExpress.XtraGrid.Columns.GridColumn colLastUpdated = new DevExpress.XtraGrid.Columns.GridColumn();
            colLastUpdated.Caption = "Cập nhật gần nhất";
            colLastUpdated.FieldName = "LastUpdated";
            colLastUpdated.Name = "colLastUpdated";
            colLastUpdated.Visible = true;
            colLastUpdated.DisplayFormat.FormatString = "dd/mm/yyyy hh:mm";
            colLastUpdated.VisibleIndex = 5;

            this.gridViewDsDoiTuong.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
                colMa,colTen, colNhomVatTuHangHoa, colGia, colLastUpdated});

            this.gridDSDoiTuong.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] {
            repLookupTaiKhoan});

            colGia.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            colGia.DisplayFormat.FormatString = GeneralLibs.LocalSettings.HeThong.NumbericFormatString;
        }

        public override void LoadData()
        {
            try
            {
                gridDSDoiTuong.DataSource = DanhMucVatTuHangHoa.GetDanhMucDichVu().DataView;
            }
            catch
            {
            }
        }
        public override void CreateNew()
        {
            try
            {
                FrmEditDichVu frm = new FrmEditDichVu((VatTuHangHoa)null);
                frm.ShowDialog();
            }
            catch
            {
            }
        }
        protected override void Edit()
        {
            try
            {
                int[] tmp = gridViewDsDoiTuong.GetSelectedRows();

                if (tmp != null && tmp.Length > 0)
                {
                    int selectedRowIndex = tmp[0];
                    object row = gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (row != null && row is DataRowView)
                    {
                        Guid id = (Guid)((DataRowView)row)[0];
                        VatTuHangHoa DanhMuc = DanhMucVatTuHangHoa.Instance.GetByID(id);
                        FrmEditDichVu frm = new FrmEditDichVu(DanhMuc);
                        frm.ShowDialog();
                    }
                }
            }
            catch
            {
            }
        }

        protected override void Delete()
        {
            try
            {

                int[] tmp = gridViewDsDoiTuong.GetSelectedRows();

                if (tmp != null && tmp.Length > 0)
                {
                    int selectedRowIndex = tmp[0];
                    object row = gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (row != null && row is DataRowView)
                    {
                        Guid id = (Guid)((DataRowView)row)[0];
                        VatTuHangHoa cv = DanhMucVatTuHangHoa.Instance.GetByID(id);
                        if (cv != null)
                        {
                            if (MessageBox.Show("Bạn có thực sự muốn xóa VatTuHangHoa", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.No)
                                return;
                            else
                                cv.DeleteFromDB();
                        }
                    }

                }
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}

